/**
 * 2018年5月20日下午11:57:02
 */
package com.an.nopro.app.framework.schedulejob;

import java.util.Set;

import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.impl.matchers.GroupMatcher;
import org.quartz.impl.triggers.CronTriggerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 
 * @目的：quartz控制器，测试任务的暂停等操作.<br>
 * 
 * @author 86171
 * @date 2018年5月20日下午11:57:02
 * 
 */
@RestController
public class QuartzController {

	@Autowired
	private SchedulerFactoryBean schedulerFactoryBean;

	/**
	 * @return 目的：更新任务执行周期 .<br>
	 * @throws Exception
	 * 
	 */
	@GetMapping("updatecron")
	public String updateCron() throws Exception {
		GroupMatcher<JobKey> groupMatcher = GroupMatcher.groupContains("");
		Set<JobKey> ss = schedulerFactoryBean.getScheduler().getJobKeys(groupMatcher);
		Scheduler scheduler = schedulerFactoryBean.getScheduler();
		for (JobKey jobKey : ss) {
			JobDetail detail = scheduler.getJobDetail(jobKey);
			scheduler.deleteJob(jobKey);

			CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
			tigger.setJobDetail(detail);
			tigger.setCronExpression("0/5 * * * * ?");// 初始时的cron表达式
			tigger.setName("srd-chhliu");// trigger的name

			CronTriggerImpl trigger = new CronTriggerImpl();
			trigger.setCronExpression("0/5 * * * * ?");
			trigger.setName("tn01");
			trigger.setGroup("tg01");
			scheduler.scheduleJob(detail, trigger);
		}
		System.out.println("jobKey:" + ss);
		return "更新任务执行周期";

	}
}
